package com.wego.controller;

import com.wego.bean.PageBean;
import com.wego.bean.ResultBean;
import com.wego.converter.UserLikeRelationConverter;
import com.wego.domain.dto.UserLikeRelationDTO;
import com.wego.domain.entity.UserLikeRelation;
import com.wego.domain.query.UserLikeRelationQuery;
import com.wego.domain.vo.UserLikeRelationVO;
import com.wego.service.UserLikeRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户点赞(tb_userLikeRelation)表控制层
 *
 */
@RestController
@RequestMapping("/userLikeRelation")
public class UserLikeRelationController {
    @Autowired
    private UserLikeRelationService userLikeRelationService;
    @Autowired
    private UserLikeRelationConverter userLikeRelationConverter;


    @GetMapping("/page")
    public ResultBean<PageBean<UserLikeRelationVO>> page(@RequestBody UserLikeRelationQuery userLikeRelationQuery) {
        final PageBean<UserLikeRelation> userLikeRelationPageBean = userLikeRelationService.getPage(userLikeRelationQuery);
        final PageBean<UserLikeRelationVO> pageBean = userLikeRelationConverter.entityPageBean2voPageBean(userLikeRelationPageBean);
        return ResultBean.success(pageBean);
    }

    @PutMapping
    public ResultBean<Void> update(@RequestBody UserLikeRelationDTO userLikeRelationDTO) {
        final UserLikeRelation userLikeRelation = userLikeRelationConverter.dto2entity(userLikeRelationDTO);
        userLikeRelationService.Update(userLikeRelation);
        return ResultBean.success("修改成功!", null);
    }

    @PostMapping
    public ResultBean<Void> insert(@RequestBody UserLikeRelationDTO userLikeRelationDTO) {
        final UserLikeRelation userLikeRelation = userLikeRelationConverter.dto2entity(userLikeRelationDTO);
        userLikeRelationService.insert(userLikeRelation);
        return ResultBean.success("添加成功!", null);
    }

    @DeleteMapping("{id}")
    public ResultBean<Void> delete(@PathVariable("id") Integer id) {
        userLikeRelationService.deleteById(id);
        return ResultBean.success("删除成功!", null);
    }

    @PostMapping("/isLike")
    public ResultBean<Void> isLike(@RequestBody UserLikeRelationDTO userLikeRelationDTO) {
        if (userLikeRelationDTO == null || userLikeRelationDTO.getUserId() == null || userLikeRelationDTO.getEntityId() == null || userLikeRelationDTO.getEntityType() == null) {
            return ResultBean.error("参数为空!", null);
        }
        if (userLikeRelationDTO.getUserId()<=0 || userLikeRelationDTO.getEntityId()<=0){
            return ResultBean.error("参数错误!", null);
        }

        boolean result = userLikeRelationService.like(userLikeRelationDTO);

        if (result) {
            return ResultBean.error("未点赞", null);
        } else {
            return ResultBean.success("已点赞", null);
        }
    }
    @PostMapping("like")
    public ResultBean<Void> like(@RequestBody UserLikeRelationDTO userLikeRelationDTO) {
        if (userLikeRelationDTO == null || userLikeRelationDTO.getUserId() == null || userLikeRelationDTO.getEntityId() == null || userLikeRelationDTO.getEntityType() == null) {
            return ResultBean.error("参数为空!", null);
        }
        if (userLikeRelationDTO.getUserId()<=0 || userLikeRelationDTO.getEntityId()<=0){
            return ResultBean.error("参数错误!", null);
        }
        boolean result = userLikeRelationService.like(userLikeRelationDTO);

        if (result) {
            return ResultBean.success("点赞成功!", null);
        } else {
            return ResultBean.error("已点赞，不能重复点赞!", null);
        }
    }
    
    @PostMapping("unlike")
    public ResultBean<Void> unlike(@RequestBody UserLikeRelationDTO userLikeRelationDTO) {
        if (userLikeRelationDTO == null || userLikeRelationDTO.getUserId() == null || userLikeRelationDTO.getEntityId() == null || userLikeRelationDTO.getEntityType() == null) {
            return ResultBean.error("参数为空!", null);
        }
        if (userLikeRelationDTO.getUserId()<=0 || userLikeRelationDTO.getEntityId()<=0){
            return ResultBean.error("参数错误!", null);
        }
        boolean result = userLikeRelationService.unlike(userLikeRelationDTO);
        
        if (result) {
            return ResultBean.success("取消点赞成功!", null);
        } else {
            return ResultBean.error("未点赞，不能取消点赞!", null);
        }
    }
}